import Sequelize from 'sequelize'
import sequelize from './../../../database'

const ManualOrder = sequelize.define('original_order',{
    id:{
        type: Sequelize.UUIDV4,
        defaultValue: Sequelize.UUIDV4,
        unique: true,
        allowNull: false,
        primaryKey:true
    },
    order_no:{
        type: Sequelize.STRING,
        unique:true,
        allowNull:false
    },
    distribution_date:{
        type:Sequelize.DATE,
        allowNull:false
    },
    customer_unit_id:{
        type:Sequelize.STRING,
        allowNull:false
    },
    total:{
        type:Sequelize.DOUBLE
    },
    is_verify: {
        type: Sequelize.INTEGER,
        defaultValue: 0 //未审核
    },
    order_status:{
      type: Sequelize.INTEGER,
      defaultValue: 1 //下单
    },
    print_count: {
        type: Sequelize.INTEGER,
        defaultValue: 0 //打印0次
    },
    verify_man:{
        type:Sequelize.STRING
    },
    verify_at: {
        type: Sequelize.DATE
    },
    memo: {
        type: Sequelize.STRING
    },
    status: {
        type: Sequelize.INTEGER,
        defaultValue: 0,
        allowNull: false
    },
    created_man: {
        type: Sequelize.STRING,
        allowNull: false
    }
},{
    tableName: 'original_order',
    updatedAt:'updated_at',
    createdAt:'created_at'
})
class ManualOrderModel{
    async insert(record){
        return await ManualOrder.create(record);
    }
    isVerify(id){
        return sequelize.query(`update original_order set is_verify=1, order_status=3 where id=${id}`);
    }
    unVerify(id){
        return sequelize.query(`update original_order set is_verify=0 where id=${id}`);
    }
    deleteRecord(id){
        return ManualOrder.destroy({id: id});
    }
    update(record){
        return ManualOrder.update(record,
            {
                'where':{
                    'id': record.id
                },
                fields: ['memo','distribution_date', 'customer_unit_id', 'total']
            }
        )
    }
    async findById(id){
        return await sequelize.query(`
            select oo.*, cu.customer_unit_name, cg.customer_group_name
            from 
            original_order oo, customer_unit cu, customer_group cg
            where 
            oo.id = '${id}' and cu.id = oo.customer_unit_id and cg.id = cu.customer_group_id`,
            { type: sequelize.QueryTypes.SELECT });
    }
    // nextRecord(id){
    //     return sequelize.query(`
    //         select oo.*, cu.customer_unit_name, cg.customer_group_name
    //         from
    //         original_order oo, customer_unit cu, customer_group cg
    //         where
    //         oo.id = '${id}' and cu.id = oo.customer_unit_id and cg.id = cu.customer_group_id`,
    //         { type: sequelize.QueryTypes.SELECT });
    // }
}

module.exports = new ManualOrderModel();